package com.example.lending.View;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lending.Model.Book;
import com.example.lending.Model.Borrow;
import com.example.lending.R;
import com.example.lending.View.Adapter.BorrowAdapter;
import com.example.lending.ViewModel.BookViewModel;
import com.example.lending.ViewModel.BorrowViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class AllBorrowFragment extends Fragment {
    List<Borrow>allBorrows;

    public AllBorrowFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_all_borrow, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final BorrowViewModel borrowViewModel;
        final BookViewModel bookViewModel=ViewModelProviders.of(getActivity()).get(BookViewModel.class);
        borrowViewModel= ViewModelProviders.of(getActivity()).get(BorrowViewModel.class);


        bookViewModel.getAllBooksLive().observe(getViewLifecycleOwner(), new Observer<List<Book>>() {
            @Override
            public void onChanged(List<Book> books) {
                BookViewModel bookViewModel1=ViewModelProviders.of(getActivity()).get(BookViewModel.class);
                bookViewModel.getAllBooksLive().observe(getViewLifecycleOwner(), new Observer<List<Book>>() {
                    @Override
                    public void onChanged(List<Book> books) {
                        RecyclerView recyclerView;
                        final BorrowAdapter borrowAdapter;
                        borrowAdapter=new BorrowAdapter(getActivity(),borrowViewModel.getAllBorrowLive().getValue(),borrowViewModel,bookViewModel,books);
                        recyclerView=getView().findViewById(R.id.rv_borrow);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
                        recyclerView.setAdapter(borrowAdapter);
                        borrowViewModel.getAllBorrowLive().observe(getActivity(), new Observer<List<Borrow>>() {
                            @Override
                            public void onChanged(List<Borrow> borrows) {
                                allBorrows=borrows;
                                borrowAdapter.setAllBorrow(borrows);
                                borrowAdapter.notifyDataSetChanged();
                            }
                        });
                        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.START|ItemTouchHelper.END) {
                            @Override
                            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                                return false;
                            }

                            @Override
                            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                                Borrow borrowToDelete = allBorrows.get(viewHolder.getAdapterPosition());
                               borrowViewModel.deleteBorrows(borrowToDelete);
                            }
                        }).attachToRecyclerView(recyclerView);
                    }
                });

            }
        });

//        new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.START|ItemTouchHelper.END) {
//            @Override
//            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
//                return false;
//            }
//
//            @Override
//            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
//                Borrow borrowToDelete = allBorrows.get(viewHolder.getAdapterPosition());
//                borrowViewModel.deleteBorrows(borrowToDelete);
//            }
//        })

    }
}
